package org.jeecg.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.wms.entity.WmsMaterialWarningLog;
import org.jeecg.modules.wms.mapper.WmsMaterialWarningLogMapper;
import org.jeecg.modules.wms.service.IWmsMaterialWarningLogService;
import org.jeecg.modules.wms.vo.WmsMaterialWarningVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 物料预警记录表Service实现类
 * @Author: jeecg-boot
 * @Date: 2023-07-17
 * @Version: V1.0
 */
@Service
public class WmsMaterialWarningLogServiceImpl extends ServiceImpl<WmsMaterialWarningLogMapper, WmsMaterialWarningLog> implements IWmsMaterialWarningLogService {

    @Autowired
    private WmsMaterialWarningLogMapper wmsMaterialWarningLogMapper;

    @Override
    public IPage<WmsMaterialWarningVO> queryWarningPageList(Page<WmsMaterialWarningVO> page, Map<String, Object> queryParam) {
        return wmsMaterialWarningLogMapper.queryWarningPageList(page, queryParam);
    }

    @Override
    public List<WmsMaterialWarningVO> selectUnhandledWarnings() {
        return wmsMaterialWarningLogMapper.selectUnhandledWarnings();
    }
    
    @Override
    public Result<?> getUnhandledWarnings() {
        try {
            List<WmsMaterialWarningVO> warnings = selectUnhandledWarnings();
            return Result.OK(warnings);
        } catch (Exception e) {
            return Result.error("获取未处理的物料预警记录失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> markWarningHandled(String id, String handleBy) {
        try {
            if (handleBy == null || handleBy.isEmpty()) {
                return Result.error("处理人不能为空");
            }
            int result = wmsMaterialWarningLogMapper.markWarningHandled(id, handleBy);
            if (result > 0) {
                return Result.OK("标记已处理成功");
            } else {
                return Result.error("标记已处理失败，预警记录不存在或已处理");
            }
        } catch (Exception e) {
            return Result.error("标记已处理失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> updatePurchaseOrderId(String id, String purchaseOrderId) {
        try {
            int result = wmsMaterialWarningLogMapper.updatePurchaseOrderId(id, purchaseOrderId);
            if (result > 0) {
                return Result.OK("关联采购单成功");
            } else {
                return Result.error("关联采购单失败，预警记录不存在");
            }
        } catch (Exception e) {
            return Result.error("关联采购单失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> refreshWarnings() {
        try {
            int result = wmsMaterialWarningLogMapper.refreshWarnings();
            return Result.OK("刷新预警成功，新增" + result + "条预警记录");
        } catch (Exception e) {
            return Result.error("刷新预警失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<?> batchUpdatePurchaseOrderId(List<String> warningIds, String purchaseOrderId) {
        try {
            int successCount = 0;
            for (String id : warningIds) {
                int result = wmsMaterialWarningLogMapper.updatePurchaseOrderId(id, purchaseOrderId);
                if (result > 0) {
                    successCount++;
                }
            }
            if (successCount > 0) {
                return Result.OK("成功关联" + successCount + "条预警记录到采购单");
            } else {
                return Result.error("关联采购单失败，预警记录不存在或已处理");
            }
        } catch (Exception e) {
            return Result.error("批量关联采购单失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<WmsMaterialWarningVO> selectWarningsByIds(List<String> ids) {
        return wmsMaterialWarningLogMapper.selectWarningsByIds(ids);
    }
} 